package com.cloudalibabaprovider9004.controller;

import com.cloudalibabacomment8001.entity.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

/**
 * @Description: TODO
 * @Author zx
 * @Date 2024/11/22 14:34
 * @Version 1.0
 */
@RestController
public class ProviderController {

    @Value("${server.port}")
    private String serverPort;

    //模仿数据库存储数据
    public static HashMap<Long,String> hashMap = new HashMap<>();
    static {
        hashMap.put(1l,"鼠标");
        hashMap.put(2l,"键盘");
        hashMap.put(3l,"耳机");
    }

    @GetMapping("info/{id}")
    public JsonResult<String> msbSql(@PathVariable("id") Long id){
        JsonResult<String> result = new JsonResult();
        result.setCode(200);
        result.setData("ServerPort:" + serverPort +":"+ hashMap.get(id));
        return result;
    }

    @GetMapping("timeOut/{id}")
    public JsonResult<String> timeOut(@PathVariable("id") Long id) {
        JsonResult<String> result = new JsonResult();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            result.setCode(500);
            result.setData("ServerPort:" + serverPort + ":" + "连接超时");
            return result;
        }

        result.setCode(200);
        result.setData("ServerPort:" + serverPort + ":" + hashMap.get(id));
        return result;
    }

}
